package service.report.impl;



import hirondelle.date4j.DateTime;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dao.LoangrantDao;
import exception.Exceptions;
import service.report.RiskManagementService;
import util.MoneyCalc;

public class RiskManagementServiceImpl implements RiskManagementService{
	private LoangrantDao grantDao;
	private String searchDate;
		
	public BigDecimal getBalanceMoney(String type,String str){//总余额，个人余额，企业余额
		String select="new vo.MoneyDoubleBean(loangrant.balance)";
		String from="Loangrant as loangrant,TContract as contract"+
		" where loangrant.contractid=contract.id and contract.contractstate=1"+
		"and (loangrant.state=0 or loangrant.state=2 or loangrant.state=3)"+
		" and DATE_FORMAT(contract.startdate, '%Y-%m') <= DATE_FORMAT('" + str + "', '%Y-%m') and DATE_FORMAT('" + str + "', '%Y-%m') <= DATE_FORMAT(contract.enddate, '%Y-%m')";
		if(!type.equals("ALL")){
			from=from+" and contract.contracttype='"+type+"'";
		}
		List list=grantDao.findQuery(select, from);
		BigDecimal balance=MoneyCalc.moneyTotal(list).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
		return balance;
	}
	public BigDecimal getMoney(String str){//本期发生额
		String select="new vo.MoneyDoubleBean(loangrant.grantmoney)";
		String from="Loangrant as loangrant where (loangrant.state=0 or loangrant.state=2 or loangrant.state=3)"
		+" and DATE_FORMAT(loangrant.grantdate, '%Y-%m') = DATE_FORMAT('" + str + "', '%Y-%m')";
		List list=grantDao.findQuery(select,from);
		BigDecimal money=MoneyCalc.moneyTotal(list).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
		return money;
	}
	public BigDecimal getModeofBalanceMoney(String str,String type){//信用，保证，抵质押，农户
		String select="new vo.MoneyDoubleBean(loangrant.balance)";
		String from="Loangrant as loangrant,TContract as contract"+
		" where loangrant.contractid=contract.id and contract.contractstate=1"
				+" and (loangrant.state=0 or loangrant.state=2 or loangrant.state=3)"+type
				+" and DATE_FORMAT(contract.startdate, '%Y-%m') <= DATE_FORMAT('" + str + "', '%Y-%m') and DATE_FORMAT('" + str + "', '%Y-%m') <= DATE_FORMAT(contract.enddate, '%Y-%m')";
		List list=grantDao.findQuery(select, from);
		BigDecimal modelbalance=MoneyCalc.moneyTotal(list).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
		return modelbalance;
	}
	public BigDecimal getYearBalance(String searchDate,String type){//信用，保证，抵押，质押，农户，企业，个人年初余额
		DateTime dt=new DateTime(searchDate);
		String year=String.valueOf(dt.getYear()-1);
		String select="new vo.MoneyDoubleBean(loangrant.balance)";
		String from="Loangrant as loangrant,TContract as contract"+
		" where loangrant.contractid=contract.id and contract.contractstate=1"
				+" and (loangrant.state=0 or loangrant.state=2 or loangrant.state=3)"
				+" and DATE_FORMAT(contract.startdate, '%Y') <= DATE_FORMAT('" + year + "', '%Y') and DATE_FORMAT('" + year + "', '%Y') <= DATE_FORMAT(contract.enddate, '%Y')";
		if(!type.equals("ALL")){
			from=from+type;
		}
		List list=grantDao.findQuery(select, from);
		BigDecimal yearbalance=MoneyCalc.moneyTotal(list).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
		return yearbalance;
	}
	public BigDecimal getYearMoney(String searchDate){//比同期
		DateTime dt=new DateTime(searchDate);
		String month=String.valueOf(dt.getMonth());
		String year=String.valueOf(dt.getYear()-1);
		String str=year+"-"+month;
		String select="new vo.MoneyDoubleBean(loangrant.grantmoney)";
		String from="Loangrant as loangrant where (loangrant.state=0 or loangrant.state=2 or loangrant.state=3)"
		+" and DATE_FORMAT(loangrant.grantdate, '%Y-%m') = DATE_FORMAT('" + str + "', '%Y-%m')";
		List list=grantDao.findQuery(select,from);
		BigDecimal money=MoneyCalc.moneyTotal(list).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
		return money;
	}
	public Map<String, BigDecimal> getRiskReportMap(String searchDate)
			throws Exceptions {
		this.searchDate=searchDate;
		Map<String, BigDecimal> RiskManageMap = new HashMap<String, BigDecimal>();
		BigDecimal totalbalance=this.getBalanceMoney("ALL", searchDate);
		RiskManageMap.put("totalbalance", totalbalance);//本期期末
		
		BigDecimal totalyearbalance=this.getYearBalance(searchDate, "ALL");
		BigDecimal totaldiffer=totalbalance.subtract(totalyearbalance).setScale(2, BigDecimal.ROUND_HALF_UP);
		RiskManageMap.put("totaldiffer", totaldiffer);//本期期末比年初
		
		BigDecimal personalbalance=getBalanceMoney("1", searchDate);
		RiskManageMap.put("personalbalance", personalbalance);//个人贷款余额
		
		BigDecimal totalpersonalbalance=this.getYearBalance(searchDate, " and contract.contracttype='1'");
		BigDecimal personaldiffer=personalbalance.subtract(totalpersonalbalance).setScale(2, BigDecimal.ROUND_HALF_UP);
		RiskManageMap.put("personaldiffer", personaldiffer);//个人余额比年初
		
		BigDecimal companybalance=getBalanceMoney("2", searchDate);
		RiskManageMap.put("companybalance", companybalance);//企业贷款余额
		
		BigDecimal totalcompanybalance=this.getYearBalance(searchDate, " and contract.contracttype='2'");
		BigDecimal companydiffer=companybalance.subtract(totalcompanybalance).setScale(2, BigDecimal.ROUND_HALF_UP);
		RiskManageMap.put("companydiffer", companydiffer);//企业余额比年初
		
		BigDecimal personalNbalance=getModeofBalanceMoney(searchDate," and contract.isaboutn=1");
		RiskManageMap.put("personalNbalance",personalNbalance);//农户贷款余额
		
		BigDecimal totalpersonalNbalance=this.getYearBalance(searchDate, " and contract.isaboutn=1");
		BigDecimal personalNdiffer=personalNbalance.subtract(totalpersonalNbalance).setScale(2, BigDecimal.ROUND_HALF_UP);
		RiskManageMap.put("personalNdiffer", personalNdiffer);//农户余额比年初
		
		BigDecimal trustbalance=getModeofBalanceMoney(searchDate," and contract.modeX=1");
		RiskManageMap.put("trustbalance",trustbalance);//信用贷款余额
		
		BigDecimal totaltrustbalance=this.getYearBalance(searchDate, " and contract.isaboutn=1");
		BigDecimal trustdiffer=trustbalance.subtract(totaltrustbalance).setScale(2, BigDecimal.ROUND_HALF_UP);
		RiskManageMap.put("trustdiffer", trustdiffer);//信用余额比年初
		
		BigDecimal ensurebalance=getModeofBalanceMoney(searchDate," and contract.modeB=1");
		RiskManageMap.put("ensurebalance",ensurebalance);//第三方担保贷款余额
		
		BigDecimal totalensurebalance=this.getYearBalance(searchDate, " and contract.modeB=1");
		BigDecimal ensurediffer=trustbalance.subtract(totalensurebalance).setScale(2, BigDecimal.ROUND_HALF_UP);
		RiskManageMap.put("ensurediffer", ensurediffer);//第三方余额比年初
		
		BigDecimal pledgebalance=getModeofBalanceMoney(searchDate," and (contract.modeD=1 or contract.modeZ=1)");
		RiskManageMap.put("pledgebalance",pledgebalance);//抵押贷款余额
		
		BigDecimal totalpledgebalance=this.getYearBalance(searchDate, " and (contract.modeD=1 or contract.modeZ=1)");
		BigDecimal pledgediffer=trustbalance.subtract(totalpledgebalance).setScale(2, BigDecimal.ROUND_HALF_UP);
		RiskManageMap.put("pledgediffer", pledgediffer);//抵押余额比年初
		
		BigDecimal totalmoney=this.getMoney(searchDate);
		RiskManageMap.put("totalmoney",totalmoney);//当年累计贷款额
		
		BigDecimal totalYearmoney=this.getYearMoney(searchDate);
		BigDecimal totalmoneydiffer=totalmoney.subtract(totalYearmoney).setScale(2, BigDecimal.ROUND_HALF_UP);
		RiskManageMap.put("totalmoneydiffer", totalmoneydiffer);
		
		return RiskManageMap;
	}
	

	public LoangrantDao getGrantDao() {
		return grantDao;
	}

	public void setGrantDao(LoangrantDao grantDao) {
		this.grantDao = grantDao;
	}
	public String getSearchDate() {
		return searchDate;
	}
	public void setSearchDate(String searchDate) {
		this.searchDate = searchDate;
	}
}
